package core.games;

import core.score.ScoreStrategy;
import domain.Frame;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

import static config.Constants.NORMAL_FRAMES;

@Data
public abstract class Game implements Calculator, Cloneable {
    private List<Frame> frames = new ArrayList<>(NORMAL_FRAMES);
    private ScoreStrategy scoreStrategy;

    public Game(ScoreStrategy scoreStrategy) {
        this.scoreStrategy = scoreStrategy;
    }

    protected  boolean canAdd(){
        return this.frames.size() < NORMAL_FRAMES;
    }

    public void addFrame(Frame frame) {
        if (canAdd()) {
            this.frames.add(frame);
        }
    }

    @Override
    public int calculateScore() {
        return scoreStrategy.calculateScore(this);
    }

    @Override
    protected Game clone() {
        Game game = null;
        try {
            game = (Game) super.clone();
            // 初始化数据
            game.frames = new ArrayList<>(NORMAL_FRAMES);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return game;
    }
}
